package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.CartServiceI;
import com.dongdongshop.service.TbAddressServiceI;
import com.dongdongshop.service.TbOrderServiceI;
import com.dongdongshop.service.TbProvincesServiceI;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("tbAddressController")
public class TbAddressController {

    private Logger logger= LoggerFactory.getLogger(TbAddressController.class);

    //地址管理
    @Reference //引用dubbo服务接口，不能使用  @Autowired
    private TbAddressServiceI tbAddressServiceI;

    @Reference //引用dubbo服务接口，不能使用  @Autowired
    private TbProvincesServiceI tbProvincesServiceI;

    @Reference(check=false ,timeout=106666)
    private CartServiceI cartServiceI;


    @Reference
    private TbOrderServiceI tbOrderServiceI;

    @RequestMapping("getFindAlls")
    public String getFindAlls(Model model){

        return "home-setting-address";
    }

    @RequestMapping("getAddress")
    @ResponseBody
    public SpecificPrint getAddress(){
        List<VoAddress> voAddresses = tbAddressServiceI.getAddress();
        return SpecificPrint.response(ResultEnum.SUCCESS).setData(voAddresses);
    }

    @RequestMapping("getSheng")
    @ResponseBody
    public List<TbProvinces> getSheng(){
        return tbProvincesServiceI.getSheng();
    }

    @RequestMapping("getShi")
    @ResponseBody
    public List<TbCities> getShi(String pid){
        return tbProvincesServiceI.getShi(pid);
    }

    @RequestMapping("getXian")
    @ResponseBody
    public List<TbAreas> getXian(String cid){
        return tbProvincesServiceI.getXian(cid);
    }

    //增加
    @RequestMapping("addTbAddress")
    @ResponseBody
    public SpecificPrint addTbAddress(TbAddress tbAddress){
        try {
            tbAddress.setCreateDate(new Date());
            TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
            tbAddress.setUserId(user.getUsername());
            tbAddress.setIsDefault("0");
            tbAddressServiceI.addTbAddress(tbAddress);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbAddress);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //删除
    @RequestMapping("deleteAddress")
    @ResponseBody
    public SpecificPrint deleteAddress(Long id){
        try {
            tbAddressServiceI.deleteAddress(id);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(id);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //修改默认
    @RequestMapping("updateAddress")
    @ResponseBody
    public SpecificPrint updateAddress(Long id){
        try {
            TbAddress tbAddresses = tbAddressServiceI.selectTbAddressesById(id);
            tbAddresses.setIsDefault("1");
            tbAddressServiceI.updateAddress(tbAddresses);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(id);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }

    //跳转结账页
    @RequestMapping("getOrderInfo")
    public String getOrderInfo(Model model){
        //先获取当前登录的用户
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        if(user == null){
            return "请先登录";
        }
        List<VoAddress> addresses =  tbAddressServiceI.getAddresss(user.getUsername());
        model.addAttribute("addressVo",addresses);

        //根据用户名获取购物车信息
        List<Cart> cartList =  cartServiceI.findCartListFromRedis(user.getUsername());
        //定义商品总价格
        double totalFees = 0;
        //定义商品总件数
        int num = 0;
        for (Cart cart : cartList) {
            //从redis中获取购物车数据
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            for (TbOrderItem tbOrderItem : orderItemList) {
                num++;
                BigDecimal totalFee = tbOrderItem.getTotalFee();
                totalFees += totalFee.doubleValue();
            }
        }
        model.addAttribute("totalFees",totalFees);
        model.addAttribute("num",num);
        model.addAttribute("cartList",cartList);
        return "getOrderInfo";
    }

/*
    //查询
    @RequestMapping("getAddresss")
    @ResponseBody
    public SpecificPrint getAddresss(){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        try {
            List<VoAddress> addresses =  tbAddressServiceI.getAddresss(user.getUsername());
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(addresses);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }*/


    @RequestMapping("insertorder")
    @ResponseBody
    public SpecificPrint insertorder(Long id,String paymentType){
        try {
            TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
            //根据id查询收货人的省市县和地址
            VoAddress addressVo =  tbAddressServiceI.selectInfoAddressById(id);
            tbOrderServiceI.insertorder(user.getUsername(),addressVo,paymentType);
            return SpecificPrint.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }
}
